package com.jinbo.footmessage.login;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import com.jinbo.footmessage.BaseActivity;
import com.jinbo.footmessage.R;
import com.jinbo.footmessage.httpbean.UserInformationBean;
import com.jinbo.footmessage.httpbean.SendCodeBean;
import com.jinbo.footmessage.httpretrofit.BusinessController;
import com.jinbo.footmessage.httpretrofit.Listener;
import com.jinbo.footmessage.utils.DialogUtils;
import com.jinbo.footmessage.utils.TimeCount;
import com.jinbo.footmessage.utils.VerifyUtils;
import com.jinbo.footmessage.view.CrossEditText;
import com.jinbo.footmessage.view.TitleBarView;


public class RegisterActivity extends BaseActivity implements View.OnClickListener {
    private CrossEditText mPhoneEt,mPasswordEt,mCodeEt,mConfirmPasswordEdt,mUserNameEdt;
    private Button  mGetCodeBt,mSubmit;
    private String  mCodeStr;
    private String  mPhoneStr;
    private String  mSexStr="1";//男为“1”，女为“2”
    private  TimeCount time;
    private RadioGroup mSexRg;
    public static void launch(Activity activity) {
        Intent intent = new Intent(activity, RegisterActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initTitleBarView();
        initView();
        initListener();
    }
    public void initTitleBarView() {
        TitleBarView titleBarView = (TitleBarView) findViewById(R.id.titleBar);
        titleBarView.setCenterTitle("注册");
        titleBarView.setLeftBackButtonIsVisibler(true);
        titleBarView.setListener(TitleBarView.TitleBarEnum.ll_leftBackImageLayout, this);
    }
    // 初始化View
    private void initView() {

        mSexRg=(RadioGroup)findViewById(R.id.rg);
        mGetCodeBt = (Button) findViewById(R.id.get_code);
        mPhoneEt = (CrossEditText) findViewById(R.id.phone_num);
        mPasswordEt = (CrossEditText) findViewById(R.id.reg_pwd);
        mSubmit = (Button) findViewById(R.id.submit);
        mCodeEt = (CrossEditText) findViewById(R.id.code_num);
        mConfirmPasswordEdt= (CrossEditText) findViewById(R.id.cofirm_pwd);
        mUserNameEdt= (CrossEditText) findViewById(R.id.user_name);
        time = new TimeCount(120000, 1000,mGetCodeBt);//构造CountDownTimer对象

    }

    private void initListener() {
        mSexRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                mSexStr=checkedId==R.id.male?"1":"2";
            }
        });
        mSubmit.setOnClickListener(this);
        mGetCodeBt.setOnClickListener(this);
        mPhoneEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (mPhoneEt.getText().toString().trim().length() == 11) {
                        mGetCodeBt.setEnabled(true);
                } else {
                    mGetCodeBt.setEnabled(false);
                }
                submitEnable();

            }
        });
        mCodeEt.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                submitEnable();
            }
        });
        mPasswordEt.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                submitEnable();
            }
        });
        mConfirmPasswordEdt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                submitEnable();
            }
        });
        mUserNameEdt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                submitEnable();
            }
        });

    }

    void submitEnable()
    {
        if (mPhoneEt.getText().toString().trim().length() == 11
                && mPasswordEt.getText().toString().trim().length() > 0&&mConfirmPasswordEdt.getText().toString().trim().length() > 0
                && mCodeEt.getText().toString().trim().length() > 0&& mUserNameEdt.getText().toString().trim().length() > 0)
        {
            mSubmit.setEnabled(true);
        } else {
            mSubmit.setEnabled(false);
        }
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.left_back_layout:
                finish();
                break;
            case R.id.get_code:
                getCode();
                break;
            case R.id.submit:
                submitCheck();
                break;
            default:
                break;
        }
    }

    public void getCode() {
        mPhoneStr = mPhoneEt.getText().toString().trim();
      /*  if (TextUtils.isEmpty(phone)) {
            show("请输入手机号");
            return;
        } else */if (/*phone.length() < 11 && */!VerifyUtils.isPhoneNum(mPhoneStr)) {
            //弹出提示 您输入的手机号码格式错误！
            show("您输入的手机号码格式错误！");
            return;
        } else {

            //接接受验证码网络接口，若成功进行如下操作

            BusinessController.getInstance().sendCode(mPhoneStr, new Listener<SendCodeBean>() {

                @Override
                public void onStart(Object... params) {
                    showProgressDialog("请稍后...");
                }

                @Override
                public void onComplete(SendCodeBean result, Object... params) {
                    dismissProgressDialog();
                   // show("验证码已发送，请注意查收");
                    time.start();
                    //countDown(COUNT);
                    mGetCodeBt.setEnabled(false);
                    mCodeStr = result.code;
                    //mCountLL.setVisibility(View.VISIBLE);
                    show("验证码已发送，请注意查收");//+mCodeStr);
                }

                @Override
                public void onFail(String msg, Object... params) {
                    dismissProgressDialog();
                    if (TextUtils.isEmpty(msg)) {

                    } else {
                        new DialogUtils(RegisterActivity.this, "center", true, 0).setMessage(msg).setButtons(null, "确定", null).create().show();

                    }
                }
            });
        }
    }


    private void submitCheck() {
        String password = mPasswordEt.getText().toString().trim();
        String confirm=mConfirmPasswordEdt.getText().toString().trim();
        String name=mUserNameEdt.getText().toString();
        if (TextUtils.isEmpty(password) || !VerifyUtils.isPassword(password) || password.length() > 16 || password.length() < 6) {
            show("密码格式为6-16位数字或字母组合");
            return;
        }
        else if(!password.equals(confirm))
        {
            show("两次输入密码不一致");
            return;
        }
        else if(mGetCodeBt.getText().toString().equals("重新验证"))
        {
            show("验证码失效，重新验证");
            return;
        }
        else if(!mCodeStr.equals(mCodeEt.getText().toString()))
        {
            show("验证码错误，重新验证");
            time.reset();
            return;
        }
        else {


        BusinessController.getInstance().register(mPhoneStr, password, name,mSexStr,new Listener<UserInformationBean>() {
            @Override
            public void onStart(Object... params) {
                showProgressDialog("请稍后...");
            }

            @Override
            public void onComplete(UserInformationBean result, Object... params) {
                dismissProgressDialog();
                show("注册成功，返回登录");
                LoginActivity.launch(RegisterActivity.this);
            }

            @Override
            public void onFail(String msg, Object... params) {
                dismissProgressDialog();
                if (TextUtils.isEmpty(msg)) {
                } else {
                    new DialogUtils(RegisterActivity.this, "center", true,0).setMessage(msg).setButtons(null, "确定", null).create().show();

                }
            }
        });
        }
    }

}